<?php

namespace App\Admin\Controllers\Dhe;

use App\Admin\Actions\Grid\Batch\SwtDheMachine\SyncMachine;
use App\Admin\Actions\Grid\Row\SwtDheMachine\MachineDetail;
use App\Admin\Actions\Grid\Tools\SwtDheMachine\MachineSyncData;
use App\Admin\Repositories\SwtDheMachine;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;

class SwtDheMachineController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SwtDheMachine(), function (Grid $grid) {
            $grid->column('id');
            $grid->column('machine_id') -> display( function ($value) {
                return $value ? : '<span class="text-danger">暂未同步</span>';
            });
            $grid->column('machine_code');
            $grid->column('device_code')->editable(true);;
            $grid->column('machine_name');
            $grid->column('door_status')->using(['离线','在线'])->label(['danger', 'success']);
            $grid->column('power_status')->using(['断电','通电','未知'])->label(['danger', 'success', 'warning']);
            $grid->column('sale_status')->using(['禁用','启用'])->label(['danger', 'success']);
            $grid->column('online_status')->using(['离线','在线'])->label(['danger', 'success']);
            $grid->column('last_online_time') -> display( function ($value) {
                return $value ?? '-';
            });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append(new MachineDetail());
                if(!$this->machine_id) {
                    $actions->disableDelete(false);
                }
            });

            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('machine_id', '是否关联', [
                    '未关联',
                    '已关联',
                ], function ($query, $value) {
                    if($value) {
                        $query->whereNotNull('machine_id');
                    } else {
                        $query->whereNull('machine_id');
                    }
                });
            });

            $grid->disableRowSelector(false);

            $grid->rowSelector()->disable(function ($row) {
                return $row->machine_id != null || $row -> device_code == null;
            });
            $grid->rowSelector()->titleColumn('id');

            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                $batch->add(new SyncMachine());
                $batch->disableDelete();
            });

            $grid->quickSearch('machine_code');

            $grid->tools(new MachineSyncData());

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SwtDheMachine(), function (Form $form) {
            $form->display('id');
            $form->text('device_code') -> rules('unique:swt_dhe_machine,device_code',['unique'=> '关联设备已存在']);
        });
    }
}
